<html>
<head><meta charset="utf-8"><title>callee.rs get_fn pointer type · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/callee.2Ers.20get_fn.20pointer.20type.html">callee.rs get_fn pointer type</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="208802990"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/callee.rs%20get_fn%20pointer%20type/near/208802990" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Hauck <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/callee.2Ers.20get_fn.20pointer.20type.html#208802990">(Sep 02 2020 at 10:14)</a>:</h4>
<p>Hi! I'm trying to understand this code - <a href="https://github.com/rust-lang/rust/blob/da897dfb6daa268a965d2d73620316ba56020a19/compiler/rustc_codegen_llvm/src/callee.rs#L47">https://github.com/rust-lang/rust/blob/da897dfb6daa268a965d2d73620316ba56020a19/compiler/rustc_codegen_llvm/src/callee.rs#L47</a> - and it appears as though the true branch of this if statement returns a <em>pointer</em> to a function, and the false branch returns the function <em>itself</em>, no pointer. This doesn't make sense, what am I missing? Or are types just inconsistent here?</p>



<a name="208803193"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/callee.rs%20get_fn%20pointer%20type/near/208803193" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/callee.2Ers.20get_fn.20pointer.20type.html#208803193">(Sep 02 2020 at 10:17)</a>:</h4>
<p><code>cx.get_declared_value(&amp;sym)</code> always returns a function pointer. It just sometimes doesn't match the expected function pointer type, so a <code>const_ptrcast</code> is sometimes necessary.</p>



<a name="208803282"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/callee.rs%20get_fn%20pointer%20type/near/208803282" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Hauck <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/callee.2Ers.20get_fn.20pointer.20type.html#208803282">(Sep 02 2020 at 10:18)</a>:</h4>
<p>Yeah, it looks like the true branch does return a function pointer, but the false branch (that does not involve <code>get_declared_value</code>, where it returns <code>None</code>), appears to return the function itself.</p>



<a name="208803304"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/callee.rs%20get_fn%20pointer%20type/near/208803304" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/callee.2Ers.20get_fn.20pointer.20type.html#208803304">(Sep 02 2020 at 10:18)</a>:</h4>
<p>I thought you were talking about the <code>if cx.val_ty(llfn) != llptrty {</code>.</p>



<a name="208803329"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/callee.rs%20get_fn%20pointer%20type/near/208803329" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Hauck <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/callee.2Ers.20get_fn.20pointer.20type.html#208803329">(Sep 02 2020 at 10:19)</a>:</h4>
<p>ah no, the if statement that I linked - in other words, it looks like <code>cx.declare_fn(&amp;sym, &amp;fn_abi)</code> returns the function itself.</p>



<a name="208803445"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/callee.rs%20get_fn%20pointer%20type/near/208803445" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/callee.2Ers.20get_fn.20pointer.20type.html#208803445">(Sep 02 2020 at 10:20)</a>:</h4>
<p>In LLVM the function is always referred to by a <code>Value</code> that can also act as function pointer I think.</p>



<a name="208803454"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/callee.rs%20get_fn%20pointer%20type/near/208803454" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Ashley Hauck <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/callee.2Ers.20get_fn.20pointer.20type.html#208803454">(Sep 02 2020 at 10:20)</a>:</h4>
<p>my only thought right now is that <code>LLVMRustGetOrInsertFunction</code> (which is what <code>declare_fn</code> boils down to) does something unexpected, and returns a pointer to the function, instead of the function itself? - aaah right. &gt;:(</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>